import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Home',
    component: () => import(/* webpackChunkName: "home" */ '../views/Index.vue'),
    children:[
      {
        path:'/home',
        name:'Home',
        component: () => import(/* webpackChunkName: "home" */ '../views/Home.vue'),
        children:[
          
        ]
      },
      {
        path:'/like',
        name:'Like',
        component: () => import(/* webpackChunkName: "like" */ '../views/Like.vue'),
      },
      {
        path:'/userCon',
        name:'UserCon',
        component: () => import(/* webpackChunkName: "userCon" */ '../views/UserCon.vue'),
      },
      {
        path:'/voice',
        name:'Voice',
        component:() => import(/* webpackChunkName: "voice" */ '@/views/home/voice/voice.vue'),
      },
      {
        path:'/addVoice',
        name:'addVoice',
        component:() => import(/* webpackChunkName: "voice" */ '@/views/home/voice/addVoice.vue'),
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "index" */ '../views/user/login.vue'),
  }
]




const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})
// 守卫模式
router.beforeEach(function (to, from, next) {
  /*  to  将要访问的路径  form 表示从哪个路径跳转而来 next 是一个函数表示放行 */ 
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr){
    return next('/login')
  }
  next()
})
export default router
